package xin.clips.web.controller.gy;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xin.clips.common.annotation.Log;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.enums.gy.DeviceTypeEnum;
import xin.clips.system.domain.gy.GyArmParam;
import xin.clips.system.domain.gy.GyDevice;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.system.domain.gy.GyMachineParam;
import xin.clips.system.dto.GyDeviceDTO;
import xin.clips.system.service.gy.IGyArmParamService;
import xin.clips.system.service.gy.IGyDeviceService;
import xin.clips.system.service.gy.IGyMachineParamService;

/**
 * 设备管理Controller
 *
 * @author clips
 * @date 2023-01-12
 */
@Controller
@RequestMapping("/gy/device")
public class GyDeviceController extends BaseController {
    private String prefix = "gy/device";

    @Autowired
    private IGyDeviceService gyDeviceService;

    @Autowired
    private IGyArmParamService armParamService;

    @Autowired
    private IGyMachineParamService machineParamService;


    @RequiresPermissions("gy:device:view")
    @GetMapping()
    public String device() {
        return prefix + "/device";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("gy:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyDevice gyDevice) {
        startPage();
        List<GyDevice> list = gyDeviceService.selectGyDeviceList(gyDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("gy:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GyDevice gyDevice) {
        List<GyDevice> list = gyDeviceService.selectGyDeviceList(gyDevice);
        ExcelUtil<GyDevice> util = new ExcelUtil<GyDevice>(GyDevice.class);
        return util.exportExcel(list, "设备管理数据");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("gy:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyDeviceDTO gyDevice) {
        return  gyDeviceService.insert(gyDevice);
    }

    /**
     * 修改设备管理
     */
    @RequiresPermissions("gy:device:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap) {
        GyDevice gyDevice = gyDeviceService.selectGyDeviceByDeviceId(deviceId);
        GyArmParam armParam = armParamService.selectGyArmParamByDeviceId(gyDevice.getDeviceId());
        GyMachineParam machineParam = machineParamService.selectGyMachineParamByDeviceId(gyDevice.getDeviceId());
        if (armParam == null) {
            armParam = new GyArmParam();
        }
        mmap.put("armParam", armParam);
        if (machineParam == null) {
            machineParam = new GyMachineParam();
        }
        mmap.put("machineParam", machineParam);
        mmap.put("gyDevice", gyDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("gy:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyDeviceDTO deviceDTO) {
        return gyDeviceService.update(deviceDTO);
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("gy:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gyDeviceService.deleteGyDeviceByDeviceIds(ids));
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    public String detail(@RequestParam("deviceId") String deviceId, ModelMap mmap) {
        GyDevice device = gyDeviceService.selectGyDeviceByDeviceId(Long.valueOf(deviceId));
        GyMachineParam machineParam = new GyMachineParam();
        GyArmParam armParam = new GyArmParam();
        if (device.getDeviceType() == DeviceTypeEnum.MACHINE.getCode()) {
            machineParam = machineParamService.selectGyMachineParamByDeviceId(device.getDeviceId());
        } else if (device.getDeviceType() == DeviceTypeEnum.ARM.getCode()) {
            armParam = armParamService.selectGyArmParamByDeviceId(device.getDeviceId());
        }
        mmap.put("device", device);
        mmap.put("armParam", armParam);
        mmap.put("machineParam", machineParam);
        return prefix + "/detail";
    }


}
